package com.zxc.gulimall.seckill.controller;


import com.zxc.common.utils.R;
import com.zxc.gulimall.seckill.service.SeckillService;
import com.zxc.gulimall.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    //当前时间可以参加秒杀的商品信息
    @GetMapping(value = "/currentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus(){
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        return R.ok().put("data",vos);
    }

    /**
     * 根据skuId查询商品是否参加秒杀活动
     * @param skuId
     * @return
     */
    @GetMapping(value = "/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSeckilInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo to = seckillService.getSkuSeckilInfo(skuId);
        return R.ok().setData(to);
    }


    @GetMapping(value = "/kill")
    public String seckill(@RequestParam(value = "killId")String killId, @RequestParam(value = "key")String key,
                     @RequestParam(value = "num")Integer num, Model model){
        //1、判断是否登录
        //2、秒杀
        String orderSn = seckillService.kill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }
}
